package com.gz.EuchreU.Controls;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.util.HorizontalAlign;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.gz.EuchreU.Contants.CardValues;
import com.gz.EuchreU.Contants.UI;

public class ScoreBoardMenu extends MovingMenuEntity{

	AnimatedSprite mMoveButton;
	ChangeableText mRedScore;
	ChangeableText mBlueScore;
	ChangeableText mRedScores;
	ChangeableText mBlueScores;
	Text mRedLabel;
	Text mBlueLabel;
	Text mRedLabelTop;
	Text mBlueLabelTop;
	
	float offSetX,offSetY;
	float mLocalX,mLocalY;
	float w = 2*UI.getCardWidth()/3;
	boolean mHidden = true;
	
	public ScoreBoardMenu(float pOnScreenX, float pOnScreenY, float pWidth, Scene pScene) {
		super(pOnScreenX, pOnScreenY, pWidth, true, new Vector2(5*UI.getWidth()/6 - pOnScreenX, UI.getCardWidth() - UI.getHeight()));
	
		offSetX = pOnScreenX;
		offSetY = pOnScreenY;
		
		mOffScreenDuration = 1;
		mOffScreenRotation = mOnScreenRotation;
		mOffScreenScale = mOnScreenScale;
		
		
		float y = pOnScreenY + backGround.getHeight() - w;
		float x = pOnScreenX;
		mMoveButton = new AnimatedSprite(x,y,w,w,UI.getTextures().getTiledSlideButton()){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pdTouchAreaLocalX,float pdTouchAreaLocalY){
				if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
					UI.setScoreBoardTouched(true);
					mLocalX = pdTouchAreaLocalX;
					mLocalY = pdTouchAreaLocalY;
				}
				else if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE ){
					log("Local touch " + pdTouchAreaLocalX + "," + pdTouchAreaLocalY);
					OnTouchMove(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
				}
				else if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
					OnTouchUp(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
				}
				return true;
			}
		};
		
		mRedScore = new ChangeableText	(x + w + 5,	y,UI.getTextures().getFont(), "0", 50);
		mRedLabel = new Text			(x + w + 5,	y - 12,UI.getTextures().getFont(), "R", HorizontalAlign.CENTER);
		mBlueScore = new ChangeableText	(x + w + 25,y,UI.getTextures().getFont(), "0", 50);
		mBlueLabel = new Text			(x + w + 25,y - 12,UI.getTextures().getFont(), "B", HorizontalAlign.CENTER);
		
		x = pOnScreenX + pWidth/3;
		y = pOnScreenY + w;
		
		mRedScores = new ChangeableText	(x,y,UI.getTextures().getFont(), "0", 1000);
		
		
		y += 20;
		mBlueScores = new ChangeableText	(x,y,UI.getTextures().getFont(), "0", 1000);
		
		
		
		attachChild(mMoveButton);
		attachChild(mBlueScore);
		attachChild(mRedScore);
		attachChild(mRedLabel);
		attachChild(mBlueLabel);
		attachChild(mRedScores);
		attachChild(mBlueScores);
		pScene.registerTouchArea(mMoveButton);
		//Touch handler for button
	}

	public void OnTouch(TouchEvent pSceneTouchEvent){
		if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE ){
			OnTouchMove(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
		}
		else if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){
			OnTouchUp(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
		}
	}
	
	private void OnTouchUp(float pX, float pY){
		UI.setScoreBoardTouched(false);
		
		if(isOnScreen){
			Hide();
			mMoveButton.setCurrentTileIndex(0);
		}
		else{
			Show();
			mMoveButton.setCurrentTileIndex(1);
		}
	}
	
	private void OnTouchMove(float pX, float pY){
		if(UI.IsScoreBoardTouched()){
			float y = pY - backGround.getHeight() - offSetY;
			y += mMoveButton.getHeight() - mLocalY;
			float x = pX - offSetX;
			x -= mMoveButton.getHeight() - mLocalX;
			setPosition(x, y );
			log("Moving to " + x + "," + y);
			
			log("Touch: " + pX + "," + pY);
			
		}
	}
	
	public void Update()
	{
		String red = "";
		String blue = "";
		
		for (int i = 0; i < CardValues.getRedScores().size(); i++) {
			red += "" + CardValues.getRedScores().get(i).toString() + "" ;
		}
		
		for (int i = 0; i < CardValues.getBlueScores().size(); i++) {
			blue += "" + CardValues.getBlueScores().get(i).toString() + "" ;
		}
		
		red += "=" + CardValues.getRedScore();
		blue += "=" + CardValues.getBlueScore();
		
		mRedScore.setText(CardValues.getRedScore() + "");
		mBlueScore.setText(CardValues.getBlueScore() + "");
		
		mRedScores.setText(red);
		mBlueScores.setText(blue);
	}
	
	private void log(String msg){
		Log.d("ScoreBoard", msg);
	}
}
